<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *空调管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择空调！','error');
	}
}
$onepluginname='jy_kongtiaoshop';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'当前空调列表'),
	//array('op'=>'otherlist','title'=>'其他场景空调列表'),
	array('op'=>'edit','title'=>'添加空调_ADD Goods'),
);
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
//上架空调
if($_RQ['op']=='list'){
	$params=array('type <>'=>4,'nochecked'=>0,'no_showsys'=>0,'no_onebuy'=>3);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate']=intval($_RQ['pcate']);
		$this->session('pcate',intval($_RQ['pcate']));
		$return['params']['pcate']=intval($_RQ['pcate']);
    }
    if(!empty($_RQ['saletype'])){
        $params['saletype like']='%'.trim($_RQ['saletype']).'%';
        $return['params']['saletype']=$_RQ['saletype'];
    }
    if(!empty($_RQ['brand'])){
        $params['brandid']=intval($_RQ['brand']);
        $return['params']['brand']=$_RQ['brand'];
    }
    if(!empty($_RQ['label'])){
        $params['labelname like']='%;'.trim($_RQ['label']).';%';
        $return['params']['label']=$_RQ['label'];
    }
	$tip='【温馨提示】：所属商户如果为“无”，表示商户已经不存在！排序，越大越靠前！<red>空调价格解释：如果门店设置了空调价格，将以门店价格为准！规格的库存，价格也是以门店的为准！</red>';
	
	$tip .='</br>【<red>一键更新空调权限</red>】：是指新版多选权限格式的更新（之前是单选）</br>[温馨提示]：设为公共分类后，该分类其他场景共享！';
	if($_SESSION['isfounder']){
		$tip .='</br>【<red>打开图片有点慢？</red>】：最好设置一下附件设置，启用图片压缩！避免某些图片过大！操作教程：系统管理--系统设置--附件设置--启用图片压缩，压缩后图片宽度(本信息管理员可见)！';
	}
	$params['onepluginname']=$onepluginname;
	$substrlength=20;
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
    $fields=array('id','api_data','merchid','pluginname','onepluginname','star','uniacid','paixu','title','image','pcate','ccate','scate','price','status','createtime','total','salenum','unit','saletype','brandid','labelname');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc,star desc, saletype desc, id desc');
	if($list){
		foreach($list as &$item){
				$item['title']=cfc('tools')->substr($item['title'],0,$substrlength);
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate'],'uniacid'=>$_SESSION['uniacid']),'title');
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate'],'uniacid'=>$_SESSION['uniacid']),'title');
				$item['scatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['scate'],'uniacid'=>$_SESSION['uniacid']),'title');				
				$item['url']=custom_url('jy_kongtiaoshop/goods.detail//id='.$item['id'],true);
				$item['salenum2']=pdo_getsum('jy_weishop_order_goods',array('goodsid'=>$item['id']),'total')?:0;
				if($jy_merch){
					$merch=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','salecate'));
				}
				if(!$merch['merchname'] && $item['merchid']){
					$merch['merchname']='商户不存在';
					pdo_update('jy_weishop_goods',array('status'=>3),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				}
				$item['merchlogo']=$merch['logo'];
				$item['salecate']=$merch['salecate']?:'';
				if($pc_set['is_en']==1){
					$shopset=plugin('jy_weishop')->plugin_setting('shopset');
					$item['merchname']=$merch['merchname']?:$shopset['title'];
					$item['salecate']=$merch['salecate']?:'NO';
					$item['salenum2']=$item['salenum']?:$item['salenum2'];
					$item['pcatetitle']=$item['pcatetitle']?:$shopset['title'];
					$item['ccatetitle']=$item['ccatetitle']?:'NO';
					$item['scatetitle']=$item['scatetitle']?:'NO';
					$item['unit']=$item['unit']?'/'.$item['unit']:'/A';
					$item['oneplugintitle']='oneplugintitle';
				}else{
                    $item['oneplugintitle']=$item['onepluginname']?pdo_getcolumn('core_plugins',array('name'=>$item['onepluginname']),'title'):'商城';
					$item['merchname']=$merch['merchname']?:'平台自营';
					$item['salecate']=$merch['salecate']?:'无';
					$item['unit']=$item['unit']?'/'.$item['unit']:'/件';
					$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
					$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
					$item['scatetitle']=$item['scatetitle']?:'无';
				}
				if($item['salenum']>=10000){
					$wans=$item['salenum']/10000;
					$wans=sprintf('%.1f',$wans);
					$item['salenum']=$wans.'万';
				}
				$item['api_data']=json_decode($item['api_data'],true);
				$item['apisn']=$item['api_data']['sn'];
				$item['apiwfee']=$item['api_data']['wfee'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$_SESSION['page']=$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
            array('type'=>'img_string','title'=>'空调ID_Goods ID',
            'data'=>array(
                array('title'=>'编号：_ID：','name'=>'id'),
                array('title'=>'排序：_Sorting：','name'=>'paixu'),
                )
            ),
			array('type'=>'img_string','title'=>'空调名称_Goods title','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'标签：_Label：','name'=>'labelname'),
				)
			),
			array('type'=>'img_string','title'=>'空调分类_Goods group',
			'data'=>array(
				array('title'=>'一级：_Level 1：','name'=>'pcatetitle'),
				array('title'=>'二级：_Level 2：','name'=>'ccatetitle'),
				)
			),
			array('type'=>'img_string','title'=>'型号/冷负荷',
			'data'=>array(
				array('title'=>'型号：','name'=>'apisn'),
				array('title'=>'冷负荷：','name'=>'apiwfee')
				)
			),
			array('name'=>'price','title'=>'销售价','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'saletype','title'=>'空调属性_attribute','type'=>'checkbox','options'=>'saletypes'),
		),'action'=>array(
			//array('type'=>'post','op'=>'copy','title'=>'复制空调_Copy'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus0/','title'=>'一键下架_Shelves'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus2/','title'=>'一键售罄_Sold out'),
			//array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus3/','title'=>'一键加入回收站_Recycle'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'空调名称_Goods'),
			array('name'=>'pcate','width'=>15,'type'=>'select','title'=>'空调分类_Groups','options'=>'groups'),
			array('name'=>'saletype','width'=>15,'type'=>'radio','title'=>'属性_Saletype','options'=>'saletypes'),
            array('name'=>'label','width'=>15,'type'=>'select','title'=>'标签_Label','options'=>'labels','option_value'=>'title'),
		)
	);
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0);
	$groupspram['onepluginname']=$onepluginname;
	$return['groups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('id','title'),'id');
	$return['type']=$this->goods_types;
	$return['saletypes']=$this->saletypes;
    $return['brands']=pdo_getall('jy_weishop_goods_brand',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
    $labels=pdo_getall('jy_weishop_goods_label',array('uniacid'=>$_SESSION['uniacid']),array('labelname'));
	$return['labels']=array();
	foreach($labels as $lv){
		$lv['labelname']=json_decode($lv['labelname'],true);
		foreach($lv['labelname'] as $lv2){
			$return['labels'][]=$lv2;
		}
	}
	$return['htmls']=$htmls;
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
	}else{
		$merchs=array();
	}
	$merchs['0']=array('id'=>0,'merchname'=>'平台自营_Proprietary');
	$return['merchs']=$merchs;
	$shopset=plugin('jy_weishop')->plugin_setting('shopset');
	set_html('title',$shopset['title']);
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择空调！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['price'])){
		$update['price']=round($post['price'],2);
	}
	if($update){
		pdo_update('jy_weishop_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	$return=model('jy_weishop/setgoods')->edit($id,$onepluginname);
	$catelevel=$return['shopset']['level']?3:2;
	$return['htmls']['edits'][0]['list']=array(
		array('name'=>'paixu','title'=>'排序','type'=>'string','help'=>'数字越大，排名越靠前'),
		array('name'=>'title','title'=>'空调名称','type'=>'string'),
		array('name'=>'image','title'=>'空调主图','type'=>'image','help'=>'建议尺寸：宽580px*高580rpx'),
		array('name'=>'video','title'=>'空调视频','type'=>'video','help'=>'默认优选显示视频，如果想关闭，请移步基础设置-其他设置'),
		array('name'=>'images','title'=>'空调展示图','type'=>'images','help'=>'<red>前端展示会追加首图，请勿重复添加</red>，建议尺寸：宽580px*高580rpx'),
		array('name'=>'subtitle','title'=>'副标题','type'=>'text','help'=>'副标题的长度请控制在100字以内'),
		array('name'=>'shorttitle','title'=>'短标题','type'=>'string','help'=>'空调短标题 用于快递打印,以及小型热敏打印机打印'),
		array('type'=>'select'.$catelevel.'_line','name'=>'pcate','options'=>'pcates','title'=>'选择分类','help'=>'[温馨提示]：如果场景下，开启了独立空调，此处只显示本场景下的分类',
			'name2'=>'ccate','options2'=>'ccates','title2'=>'请选择二级分类',
			'name3'=>'scate','options3'=>'scates','title3'=>'请选择三级分类',
		),
		array('name'=>'saletype','title'=>'空调属性','type'=>'checkbox','options'=>'saletype'),
		array('name'=>'total','title'=>'库存','type'=>'unitstring','unit'=>'台'),
		array('name'=>'price','title'=>'价格','type'=>'string','help'=>''),
		array('name'=>'costprice','title'=>'成本价','type'=>'string','show'=>array('supplier_id'=>0)),
		array('name'=>'costprice','title'=>'供应价','type'=>'copy','show'=>array('supplier_id >'=>0),'help'=>'由供货商发布空调，设置供应价格'),
		array('name'=>'no_oldprice','title'=>'不显示原价？','type'=>'switch'),
		array('name'=>'originalprice','title'=>'原价','type'=>'string','show'=>array('no_oldprice'=>0)),
		array('name'=>'oldpricetitle','title'=>'原价自定义','type'=>'string','show'=>array('no_oldprice'=>0),'help'=>'例如:市场价、京东价、天猫价、苏宁价、淘宝价'),
		array('name'=>'no_oldpriceline','title'=>'不显示原价删除线？','type'=>'switch','show'=>array('no_oldprice'=>0),'help'=>'请填写真实市场价/同行价，方便后台统计节省数据'),
		array('name'=>'marketprice','title'=>'市场价','type'=>'string','help'=>'请填写真实市场价/同行价，方便后台统计节省数据'),
		array('name'=>'seven','title'=>'退换货','type'=>'switch','help'=>'支持单空调退换货后，需移步设置-退款设置-设置好退款信息！才能生效！'),
		array('name'=>'status','title'=>'上架','type'=>'switch'),
	);
	$return['htmls']['edits'][1]=array(
		'title'=>'空调参数',
		'list'=>array(
			array('title'=>'当前参数','type'=>'title'),
			array('name'=>'api_data.sn','title'=>'型号','type'=>'string','help'=>'例如：RFTS28MXS-D1'),
			array('name'=>'api_data.wfee','title'=>'冷负荷(W)','type'=>'string','help'=>'例如：2800'),
		)
	);
	$return['htmls']['tabs']=array(
		array('op'=>'list','title'=>'返回空调列表'),
	);
	$return['types']=array('内机','外机');
	unset($return['htmls']['edits'][2]);
	unset($return['htmls']['edits'][3]);
	unset($return['htmls']['edits'][5]);
	unset($return['htmls']['edits'][6]);
	unset($return['htmls']['edits'][7]);
	unset($return['htmls']['edits'][8]);
	unset($return['htmls']['edits'][9]);
	unset($return['htmls']['edits'][10]);
	unset($return['htmls']['edits'][11]);
	unset($return['htmls']['edits'][12]);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=model('jy_weishop/setgoods')->postdata($postdata);
	$goodsid=intval($postdata['id']);
	if($goodsid){
		pdo_update('jy_weishop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['onepluginname']=$onepluginname?:'';
		$data['pluginname']=$onepluginname?:'';
		$data['no_onebuy']=3;
		$data['unit']='台';
		pdo_insert('jy_weishop_goods',$data);
		$goodsid=pdo_insertid();
	}
	model('jy_weishop/setgoods')->postup($postdata,$goodsid);
	exi('操作成功！','','goback');
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('jy_weishop_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			pdo_delete('jy_weishop_goods_spec',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id));
			pdo_delete('jy_weishop_goods_spec_item',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id));
			pdo_delete('jy_weishop_goods_option',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id));
			pdo_delete('jy_weishop_goods_dispatch',array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$id));
			model('jy_weishop/store')->delgoods($id);
		}
	}
	exi('删除成功！');
}